package cn.zyq.demo.controller.mgt.system;


import cn.zyq.demo.beans.sys.SysConfig;
import cn.zyq.demo.service.sys.SysConfigService;
import cn.zyq.demo.system.annotation.Log;
import cn.zyq.demo.system.common.BaseController;
import cn.zyq.demo.system.common.PageParam;
import cn.zyq.demo.system.exception.BusinessException;
import cn.zyq.demo.system.ry.enums.BusinessType;
import cn.zyq.demo.system.ry.util.poi.ExcelUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 参数配置 信息操作处理
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/mgt/system/config")
public class SysConfigController extends BaseController {
    @Autowired
    private SysConfigService configService;

    /**
     * 获取参数配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:config:list')")
    @PostMapping("/list")
    public Object list(@RequestBody PageParam param) {
        PageHelper.startPage(param.getPage(), param.getPageSize());
        List<SysConfig> list = configService.selectConfigList(param);
        return new PageInfo<>(list);
    }

    @Log(title = "参数管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:config:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody PageParam param) {
        List<SysConfig> list = configService.selectConfigList(param);
        ExcelUtil<SysConfig> util = new ExcelUtil<SysConfig>(SysConfig.class);
        util.exportExcel(response, list, "参数数据");
    }

    /**
     * 根据参数编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:config:query')")
    @GetMapping(value = "/{configId}")
    public Object getInfo(@PathVariable Integer configId) {
        return configService.selectConfigById(configId);
    }

    /**
     * 根据参数键名查询参数值
     */
    @GetMapping(value = "/configKey/{configKey}")
    public Object getConfigKey(@PathVariable String configKey) {
        return configService.selectConfigByKey(configKey);
    }

    /**
     * 新增参数配置
     */
    @PreAuthorize("@ss.hasPermi('system:config:add')")
    @Log(title = "参数管理", businessType = BusinessType.INSERT)
    @PostMapping
    public void add(@Validated @RequestBody SysConfig config) {
        if (!configService.checkConfigKeyUnique(config)) {
            throw new BusinessException("新增参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }

        this.configService.insertConfig(config);
    }

    /**
     * 修改参数配置
     */
    @PreAuthorize("@ss.hasPermi('system:config:edit')")
    @Log(title = "参数管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public void edit(@Validated @RequestBody SysConfig config) {
        if (!configService.checkConfigKeyUnique(config)) {
            throw new BusinessException("修改参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
       this.configService.updateConfig(config);
    }

    /**
     * 删除参数配置
     */
    @PreAuthorize("@ss.hasPermi('system:config:remove')")
    @Log(title = "参数管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{configIds}")
    public void remove(@PathVariable Integer[] configIds) {
        configService.deleteConfigByIds(configIds);
    }

    /**
     * 刷新参数缓存
     */
    @PreAuthorize("@ss.hasPermi('system:config:remove')")
    @Log(title = "参数管理", businessType = BusinessType.CLEAN)
    @DeleteMapping("/refreshCache")
    public void refreshCache() {
        this.configService.resetConfigCache();
    }
}
